package com.school.gmz.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.gmz.common.Result;
import com.school.gmz.entity.Lab;
import com.school.gmz.mapper.LabMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/lab")
@Api(tags = "实验室管理") // 定义API的分类
public class LabController {
    @Resource
    LabMapper labMapper;

    // 添加实验室
    @PostMapping
    @ApiOperation(value = "添加实验室", notes = "创建一个新的实验室记录")
    public Result<?> save(@RequestBody Lab lab){
        labMapper.insert(lab);
        return Result.success();
    }

    // 更新实验室
    @PutMapping
    @ApiOperation(value = "更新实验室", notes = "根据ID更新实验室记录")
    public Result<?> update(@RequestBody Lab lab){
        labMapper.updateById(lab);
        return Result.success();
    }

    // 删除实验室
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除实验室", notes = "根据ID删除实验室记录")
    public Result<?> delete(@ApiParam(value = "实验室ID", required = true) @PathVariable Long id){
        labMapper.deleteById(id);
        return Result.success();
    }

    // 查询实验室
    @GetMapping
    @ApiOperation(value = "查询实验室", notes = "支持位置搜索")
    public Result<?> findPage(@ApiParam(value = "位置", defaultValue = "") @RequestParam(defaultValue = "") String site){
        LambdaQueryWrapper<Lab> wrapper = Wrappers.<Lab>lambdaQuery();
        if (StrUtil.isNotBlank(site)){
            wrapper.like(Lab::getSite, site); // 假设字段名为location
        }
        Page<Lab> labPage = labMapper.selectPage(new Page<>(1, 10), wrapper); // 默认分页参数
        return Result.success(labPage);
    }
}